class Solution 
{
public:
    int divide(int dividend, int divisor) 
    {
        if (dividend == 0)
        {
            return 0;
        }

        if (divisor == 1)
        {
            return dividend;
        }
        else if (divisor == -1)
        {
            if (dividend > INT_MIN)
            {
                return -dividend;
            }
            return INT_MAX;
        }
        else
        {
            int sign = (dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0) ? 1 : -1;
            long res = div_implement(abs(dividend), abs(divisor));
            if (sign > 0)
            {
                return res > INT_MAX ? INT_MAX : res;
            }
            else
            {
                return -res;
            }
        }

    }

    int div_implement(long dividend, long divisor)
    {
        if (dividend < divisor)
        {
            return 0;
        }

        long count = 1, tb = divisor;
        while (tb + tb <= dividend)
        {
            count = count + count;           // 最小解翻倍 
            tb = tb + tb;
        }

        return count + div_implement(dividend - tb, divisor);
    }
};